const fs = require('fs');

fs.readFile('./daoyin.lrc', (err, data) => {
    if (err) {
        console.error(err);
    } else {
        let lrc = data.toString().split('\n') //换行
        //lrc分为两部分,所以我们要用正则表达式来提取这两部分,特别是对时间的提却
        let regx = /\[(\d{2}):(\d{2})\.(\d{2})](.*)/;
        let newregx = /\[\w{2}:(.*)]/;
        //转换成数组然后通过forEach来遍历
        let start = new Date().getTime();
        lrc.forEach((value) => {
            //eval()执行完后返回结果,而exec()执行完后不返回结果
            let str = regx.exec(value);
            if (str) {
                let minute = parseFloat(str[1]);  //分钟
                let second = parseFloat(str[2]);    //第二
                let millisecond = parseFloat(str[3]);   //毫秒
                let content = str[4];       //放时间的容器
                let end = new Date().getTime(); //获取时间
                let time = minute * 60 * 1000 + second * 1000 + millisecond - (end - start);
                setTimeout(() => {
                    console.log("[" + minute + ":" + second + ":" + millisecond + "]", content); //拼接字符串
                }, time);
            } else {
                let deputy = newregx.exec(value);
                if (deputy) {
                    console.log(deputy[1]);
                }
            }
        })
    }
})